package drone;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author Kjell Bunjes, Robert Verginien Nickel
 */
public class Controller {
    
    private static Controller ctrl = null;
    private Drone drone;
    
    public Controller(Drone drone){
        this.drone = drone;
        transmitDronePositions();
    }
    
    /**
     * Singleton-Pattern is implemented
     * You need to use this instead of a constructor
     * @param drone
     * @return an object of Controller
     */
    public static Controller getInstance(Drone drone){            
        if(ctrl == null){                    
                ctrl = new Controller(drone);
                return ctrl;
        }
        return ctrl;
    }
    
    /**
     * Searches the DataBaseSpaces for the next Space with isEmpty == true
     * sets it to false and returns it.
     * @return first free Object of Space
     */
    public Space findNextSpace(){
        //System.out.println("findNextSpaceEntered!");
        DataBaseSpaces dbs = DataBaseSpaces.getInstance();
        //Collections.sort(dbs.getAllSpaces());
        for(int i = 0; i < dbs.getAllSpaces().size(); i++){
            if(dbs.getAllSpaces().get(i).isEmpty() == true){
                dbs.getAllSpaces().get(i).setEmpty(false);
                return dbs.getAllSpaces().get(i);
            }
        }
        return null;
    }

    /**
     * reads coordinates from DronePosition.txt that define startingPoint and rechargePoint
     * and transmits them to the drone
     */    
    public void transmitDronePositions(){
        BufferedReader br = null;
        try {
              String thisLine;
              br = new BufferedReader(new FileReader("DronePositions.txt"));
              String[] splitted;
              ArrayList<Coordinates> dronePositions = new ArrayList<Coordinates>();
              do {
                  thisLine = br.readLine();
                  //creates an array from the with ";" splitted line
                  splitted = thisLine.split(";");
                  //reads the xPosition from the array
                  float xPos = Float.parseFloat(splitted[0]);
                  //reads the yPosition from the array
                  float zPos = Float.parseFloat(splitted[1]);
                  //stores coordinates from .txt file into ArrayList
                  dronePositions.add(new Coordinates(xPos, zPos));
              } 
              while(br.ready());
              
              //reads startingPoint and rechargePoint from ArrayList and transmits them to drone
              Coordinates startingPoint = dronePositions.get(0);
              drone.setStartingPoint(startingPoint);
              drone.setGate(startingPoint);
              Coordinates rechargePoint = dronePositions.get(1);
              System.out.println("Recharging Point at: "+rechargePoint.getxPos()+" | "+rechargePoint.getzPos());
              drone.setRechargePoint(rechargePoint);
              drone.setPosition(rechargePoint);
              
        } catch (IOException e) {

        }
    }
    
    /**
     * submits the next task with waypoints of the next free Space to the drone
     */ 
    public void submitTask(){
        Space space = findNextSpace();
        System.out.println(space);
        ArrayList<Coordinates> waypoints = space.getWayPoints();
        System.out.println("Waypoints: " + waypoints.size());
        drone.addTask(waypoints);
    }
}
